package com.road.vast.department.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.road.vast.component.IdWorkerComponent;
import com.road.vast.department.mapper.CoDepartmentMapper;
import com.road.vast.department.service.CoDepartmentService;
import com.road.vast.pojo.company.CoDepartment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 郭泽鹏
 * @since 2020-07-10
 */
@Service
public class CoDepartmentServiceImpl extends ServiceImpl<CoDepartmentMapper, CoDepartment> implements CoDepartmentService {
    @Resource
    private CoDepartmentMapper coDepartmentMapper;

    @Autowired
    private IdWorkerComponent idWorkerComponent;

    @Override
    public Integer saveDepartment(CoDepartment coDepartment) {
        //雪花算法生成id
        String nextId = idWorkerComponent.getIdWorker().nextId() + "";
        coDepartment.setId(nextId);
        return coDepartmentMapper.insert(coDepartment);
    }

    @Override
    public Integer updateDepartment(CoDepartment coDepartment) {
        CoDepartment dept = coDepartmentMapper.selectById(coDepartment.getId());
        dept.setName(coDepartment.getName());
        dept.setCode(coDepartment.getCode());
        dept.setCategory(coDepartment.getCategory());
        dept.setCity(coDepartment.getCity());
        dept.setIntroduce(coDepartment.getIntroduce());
        dept.setManager(coDepartment.getManager());
        return coDepartmentMapper.updateById(dept);
    }

    @Override
    public CoDepartment findById(String id) {
        return coDepartmentMapper.selectById(id);
    }

    @Override
    public List<CoDepartment> findAll(String companyId) {
        QueryWrapper<CoDepartment> wrapper = new QueryWrapper<>();
        wrapper.eq("company_id", companyId);
        return coDepartmentMapper.selectList(wrapper);
    }

    @Override
    public Integer deleteCoDepartment(String id) {
        return coDepartmentMapper.deleteById(id);
    }
}
